﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace VlcPlayer
{
    public partial class Browser : PhoneApplicationPage
    {

#if !WP70

        ProgressIndicator progress;
#endif
        public Browser()
        {
            InitializeComponent();

            ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Text = AppResources.Home;
            ((ApplicationBarIconButton)ApplicationBar.Buttons[1]).Text = AppResources.EnterUrl;

           
        }



        private VlcAccess _vlc;
        private Uri _src;
        private string _svId;

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            #if !WP70
            progress = SystemTray.ProgressIndicator;

            if (progress == null)
            {
                progress = new ProgressIndicator();
                SystemTray.SetProgressIndicator(this, progress);
            }

            progress.Text = null;
            progress.IsVisible = progress.IsIndeterminate = true;
            #endif

            var src = NavigationContext.QueryString["src"];
            _svId = NavigationContext.QueryString["svId"];
            _src = new Uri(src);
            _vlc = new VlcAccess(_src.Host + ":" + _src.Port);
            _vlc.OnBrowseFileCompleted += new VlcAccess.BrowseFileCallback(_vlc_OnBrowseFileCompleted);
            _vlc.SendChangeDirCommand(_src.LocalPath.Substring(1));
        }

        void _vlc_OnBrowseFileCompleted(bool success)
        {

            Dispatcher.BeginInvoke(() =>
                                       {
#if !WP70

                                           progress.IsVisible = !success;
                                           progress.IsIndeterminate = false;
#endif
                                           if (success)
                                           {
                                               DataContext = _vlc.BrowseInfo;
                                           }
                                           else
                                           {
#if !WP70                                               
                                               progress.Text = AppResources.DirFailed;
#endif
                                           }
                                       });

            
        }

        private void ItemsControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (FileList.SelectedItem != null)
            {
                var element = (element) FileList.SelectedItem;
                
                FileList.SelectedItem = null;
                if (element.type.StartsWith("dir"))
                {
                    NavigationService.Navigate(new Uri("/Browser.xaml?svId=" + _svId + "&src=http://" + _src.Host + ":" + _src.Port+"/" + element.path, UriKind.Relative));
                }
                else if (element.type == "file")
                {
                    NavigationService.Navigate(new Uri(@"/PlayerPage.xaml?svId=" + _svId + "&src=" + element.path, UriKind.Relative));
                    //NavigationService.Navigate(new Uri(@"/StreamingClient;component/Player/Player.xaml?host=" +_src.Host + ":" + _src.Port + "&src=" + element.path, UriKind.Relative));
                }
                    
            }
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            //home
            NavigationService.Navigate(new Uri("/Browser.xaml?svId=" + _svId + "&src=http://" + _src.Host + ":" + _src.Port, UriKind.Relative));
        }

        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {
            //uri
            NavigationService.Navigate(new Uri("/AddUrl.xaml?svId=" + _svId + "&host=" + _src.Host + ":" + _src.Port, UriKind.Relative));
        }

        private void WebBrowser_NavigationFailed(object sender, System.Windows.Navigation.NavigationFailedEventArgs e)
        {

        }
    }
}